package com.jml.Lock;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 */
public class AWait {

    public static ReentrantLock reentrantLock = new ReentrantLock();
    public static Condition condition = reentrantLock.newCondition();

    public static void main(String[] args) throws InterruptedException {
        Sy1 s1 = new Sy1();
        s1.start();
        Thread.sleep(1000);
        Sy1 s2 = new Sy1();
        s2.start();
        Thread.sleep(1000);
        Sy1 s3 = new Sy1();
        s3.start();
        Thread.sleep(1000);
        Sy1 s4 = new Sy1();
        s4.start();
        Thread.sleep(1000);

        reentrantLock.lock();
        condition.signal();
        reentrantLock.unlock();
        Thread.sleep(1000);

        reentrantLock.lock();
        condition.signal();
        reentrantLock.unlock();
        Thread.sleep(1000);

        reentrantLock.lock();
        condition.signal();
        reentrantLock.unlock();
        Thread.sleep(1000);

        reentrantLock.lock();
        condition.signal();
        reentrantLock.unlock();
        Thread.sleep(1000);

        reentrantLock.lock();
        condition.signal();
        reentrantLock.unlock();
        Thread.sleep(1000);



    }

    public static class Sy1 extends Thread {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " begin");
            try {
                reentrantLock.lock();
                condition.await();
                reentrantLock.unlock();
                System.out.println(Thread.currentThread().getName() + " end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Sy2 extends Thread {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " begin");
            try {
                reentrantLock.lock();
                condition.await();
                reentrantLock.unlock();
                System.out.println(Thread.currentThread().getName() + " end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Sy3 extends Thread {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " begin");
            try {
                reentrantLock.lock();
                condition.await();
                reentrantLock.unlock();
                System.out.println(Thread.currentThread().getName() + " end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Sy4 extends Thread {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " begin");
            try {
                reentrantLock.lock();
                condition.await();
                reentrantLock.unlock();
                System.out.println(Thread.currentThread().getName() + " end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
